package cn.com.surker.workcon.service.zhcl;

import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.common.ProDelEnum;
import cn.com.surker.workcon.service.ProdDelService;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 删除派单数据 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2024-09-26
 */
public class ProdDelServiceImpl extends GKJDBCService implements ProdDelService {

	public int insert(Long lineId, Long dispatchId, String dispatchCode) {
		if (lineId == null || dispatchId == null) {
			return -1;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineTemplate = getLineJdbcTemp(line);
		//	没有数据源
		if (lineTemplate == null) {
			return -1;
		}
		String distanceCtrlTable = ZhclUtil.getCtrlTableName(line, "DistanceCtrl");
		//	删除sql
		String delSql = "delete from " + distanceCtrlTable + " where OrderID = ? and ProductStatus = 0";
		Object[] params = {dispatchId};
		//	开启事务
		beginTransaction(lineId);
		int update = lineTemplate.update(delSql, params);
		return update;
	}

	public Integer selectByDisId(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineTemplate = getLineJdbcTemp(line);
		//	没有数据源
		if (lineTemplate == null) {
			return null;
		}
		String distanceCtrlTable = ZhclUtil.getCtrlTableName(line, "DistanceCtrl");
		//	查询工控机的生产状态,珠海长路的工控派单表没有表示是否被删除状态,根据表数据判定,如果没有数据,即表示被删除
		String querySql = "select ProductStatus from " + distanceCtrlTable + " where OrderID = " + dispatchId;
		//	查询数据
		List<Map<String, Object>> maps = lineTemplate.queryForList(querySql);
		if (maps == null || maps.isEmpty()) {
			return ProDelEnum.SUCCESS.getCode();
		}
		Map<String, Object> map = maps.get(0);
		if (map == null) {
			return ProDelEnum.SUCCESS.getCode();
		}
		return -1;
	}

	public Integer deleteByDisId(Long lineId, Long dispatchId) {
		return null;
	}
}


